package com.goldcn.order.service;

import com.goldcn.common.HttpUtil;
import com.goldcn.common.model.DataResult;
import com.goldcn.common.util.IdGen;
import com.goldcn.config.ProConstants;
import com.goldcn.order.dao.OrderInfoMapper;
import com.goldcn.order.dao.OrderItemMapper;
import com.goldcn.order.dto.OrderIndexDto;
import com.goldcn.order.dto.OrderItemIndexDto;
import com.goldcn.order.model.OrderInfo;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/6/6.
 */
@Service
public class OrderDeleteService {
    @Autowired
    OrderInfoMapper orderInfoMapper;

    @Autowired
    OrderItemMapper orderItemMapper;

    @Autowired
    OrderIndexService orderIndexService;

    @Autowired
    RestTemplate restTemplate;


    private static Logger logger = LoggerFactory.getLogger(OrderDeleteService.class);

    public Object deleteOrderAndItem(List<Long> list) {
        if (list != null && list.size() != 0) {
            List<OrderInfo> orderInfoList = orderInfoMapper.selectByIdList(list);
            if (orderInfoList != null && orderInfoList.size() != 0) {
                for (OrderInfo info : orderInfoList) {
                    orderItemMapper.deleteByOrderID(info.getOrderID());
                    orderIndexService.deleteIndex(info.getOrderID());
                }
                orderInfoMapper.deleteByList(list);
            }
        }
        return "success";
    }

    public Object buildPaymentByPointFreight() {

        String url = ProConstants.POINT+"/golden/withdrawList/condition/1/9999";
        JSONObject para = new JSONObject();
        para.put("withdrawType", "1");
        //para.put("withdrawStatus", new int[]{2});

        JSONObject result = HttpUtil.httpPOSTJSONObject(url, para, restTemplate,true);
        if (result.getInt("isSuccess") != 1) {
            logger.error("not found data.");
        }
        List<JSONObject> objectList = result.getJSONObject("data").getJSONArray("list");
        String tempPayNO, payNO;
        for (JSONObject jsonObj : objectList) {

            OrderIndexDto freight = new OrderIndexDto();

            tempPayNO = jsonObj.getString("pdpayNo");
            payNO = tempPayNO;
            if (tempPayNO != null && tempPayNO.indexOf("X") > 0) {
                payNO = tempPayNO.substring(0, tempPayNO.indexOf("X"));
            }

            freight.setPayNO(tempPayNO);
            freight.setOrderID(IdGen.get().nextId() + "");
            freight.setOrderNO(payNO);
            freight.setAmount(new BigDecimal(jsonObj.getString("fee")));

            JSONObject pdextJSON = JSONObject.fromObject(jsonObj.getString("pdextJSON"));
            if (pdextJSON != null && pdextJSON.get("payTime") != null) {
                freight.setPayTime(pdextJSON.getString("payTime"));
            }
            freight.setPayType(4);//wechat jsPay
            freight.setBuyersName(jsonObj.getString("receipt"));
            freight.setAddress(jsonObj.getString("address"));
            freight.setMobile(jsonObj.getString("telephone"));
            freight.setOrderType("2");
            freight.setStatus(2);
            freight.setBuyersID(jsonObj.getString("pduserId"));
            freight.setAddTime((jsonObj.getString("pdhandleTime")));
            freight.setOrderItem(new ArrayList<OrderItemIndexDto>());
            DataResult resultFreight = HttpUtil.httpPost(ProConstants.ESURL + "/order/newIndex", freight, restTemplate);
            if (resultFreight.getIsSuccess() != 1) {
                logger.error("提金运保费订单新建索引失败,orderID=" + payNO + ",message:" + resultFreight.getErrorDesc());
            }
        }
        return "success";
    }

}
